import { DataStore } from "@core/nedb/db";
import { reduce } from "rxjs";

class RepositoryBase<T> {
  private db: DataStore<T>;

  constructor() {
    this.db = new DataStore();
  }

  protected async add(record: T): Promise<T> {
    return await this.db.add(record).then(() => {
      return record;
    });
  }

  protected async update(oldRecord: T, newRecord: T): Promise<T> {
    return await this.db.update(oldRecord, newRecord).then(() => {
      return newRecord;
    });
  }

  protected async delete(record: T): Promise<void> {
    return await this.db.delete(record);
  }

  protected async find(query: object): Promise<T[]> {
    return await this.db.find(query);
  }

  protected async findOne(query: object): Promise<T> {
    return await this.db.findOne(query);
  }
}

export { RepositoryBase };
